package com.cq.hd.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cq.hd.common.constant.Constant;
import com.cq.hd.common.enums.biz.PayStateEnum;
import com.cq.hd.common.pagination.Page;
import com.cq.hd.common.response.Throw;
import com.cq.hd.common.utils.LocalDateUtil;
import com.cq.hd.common.utils.StringUtils;
import com.cq.hd.member.api.dto.AdminMemberCardBillPageDto;
import com.cq.hd.member.api.vo.AdminMemberCardBillPageVo;
import com.cq.hd.member.po.TbMemberCardBillPo;
import com.cq.hd.member.mapper.TbMemberCardBillMapper;
import com.cq.hd.member.service.TbMemberCardBillService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 次卡下单流水表 服务实现类
 * </p>
 *
 * @author mYunYu
 * @since 2023-12-21
 */
@Service
public class TbMemberCardBillServiceImpl extends ServiceImpl<TbMemberCardBillMapper, TbMemberCardBillPo> implements TbMemberCardBillService {

    @Override
    public Page<AdminMemberCardBillPageVo> pageAdminMemberCardBill(AdminMemberCardBillPageDto adminMemberCardBillPageDto) {
        String payTimeStart = adminMemberCardBillPageDto.getPayTimeStart();
        if (!StringUtils.isBlank(payTimeStart)) {
            try {
                LocalDateUtil.toLocalDateTime(payTimeStart, Constant.YYYY_MM_DD_HH_MM_SS);
            } catch (Exception e) {
                Throw.isBusinessException("购买开始时间格式错误");
            }
        }

        String payTimeEnd = adminMemberCardBillPageDto.getPayTimeEnd();
        if (!StringUtils.isBlank(payTimeEnd)) {
            try {
                LocalDateUtil.toLocalDateTime(payTimeEnd, Constant.YYYY_MM_DD_HH_MM_SS);
            } catch (Exception e) {
                Throw.isBusinessException("购买结束时间格式错误");
            }
        }

        PageHelper.startPage(adminMemberCardBillPageDto.getPageNum(), adminMemberCardBillPageDto.getPageSize());
        List<AdminMemberCardBillPageVo> adminMemberCardBillPageVos = baseMapper.listAdminMemberCardBill(adminMemberCardBillPageDto);
        if (!CollectionUtils.isEmpty(adminMemberCardBillPageVos)) {
            for (AdminMemberCardBillPageVo adminMemberCardBillPageVo : adminMemberCardBillPageVos) {
                adminMemberCardBillPageVo.setReturnNo(adminMemberCardBillPageVo.getHfReqSeqId());
            }
        }
        PageInfo<AdminMemberCardBillPageVo> pageInfo = new PageInfo<>(adminMemberCardBillPageVos);
        return Page.newPageWithAllFields(pageInfo.getList(), pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal(), pageInfo.getPages());
    }

    @Override
    public Boolean removeWaitPayJob() {
        baseMapper.delete(new QueryWrapper<TbMemberCardBillPo>().eq("pay_state", PayStateEnum.WAIT.getValue())
                .le("order_time", LocalDateTime.now().plusDays(-2)));
        return true;
    }

    @Override
    public BigDecimal getTotalAmt() {
        return baseMapper.getTotalAmt(PayStateEnum.SUCCESS.getValue());
    }
}
